<?
require("mailer/mailer.php");
require("includes/funciones.inc.php");

function sendMailContact()
{
	require("includes/selectLanguage.php");
	require("includes/constants.php");

	try
	{				
		//Make paramData with Request
		$paramData = "Name=".urlencode($_REQUEST["frmContacto_Name"]).
					 "&Resto=".urlencode($_REQUEST["frmContacto_Resto"]).
					 "&EMail=".urlencode($_REQUEST["frmContacto_EMail"]).
					 "&Phone=".urlencode($_REQUEST["frmContacto_Phone"]).
					 "&Message=".urlencode($_REQUEST["frmContacto_Message"]);
					 
		//Make addresses
		$addresses = array();
		array_push($addresses, "dpisarello@gmail.com");
		//array_push($addresses, "fpisarello@gmail.com");
		array_push($addresses, "tolesole@hotmail.com");
		
		enviarMailConPagina($addresses, $a_languages['Mailer.Subject'], $basedir . $mailContactUrl, $paramData);
		
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Mailer.SendOKContact'];
		return json_encode($arrJSON);
			
	}
	catch(Exception $e) {
		$arrJSON->status = "ERR";
		$arrJSON->msg = $a_languages['Mailer.SendErrorContact'];
		return json_encode($arrJSON);
	}	
}

function sendMailDemo()
{

	require("includes/selectLanguage.php");
	require("includes/constants.php");

	$mcrypt = new MCrypt();

	try
	{
		$DB = NewADOConnection('mysqlt');
		$DB->Connect();
		$DB->Execute("SET NAMES utf8;");
		//$DB->debug=true;
		
		$userName = substr($_REQUEST["frmDemo_EMail"], 0, stripos($_REQUEST["frmDemo_EMail"], '@'));

		$strSQL = "SELECT userName FROM users WHERE userName = '".$userName."'";
		$rsUsers = $DB->Execute($strSQL);

		if(!$rsUsers->EOF)
		{
			$arrJSON->status = "ERR";
			$arrJSON->msg = $a_languages['Mailer.Demo.SendErrorUserDuplicate'];
			return json_encode($arrJSON);
		}
		
		$DB->StartTrans(); // iniciamos transaccion
		
		//Add Restaurant
		$strSQL = "INSERT INTO restaurant ( ";
		$strSQL .= "	businessName, ";
		$strSQL .= "	otherName, ";
		$strSQL .= "	address, ";
		$strSQL .= "	zipCode, ";
		$strSQL .= "	phone, ";
		$strSQL .= "	fax, ";
		$strSQL .= "	otherPhone, ";
		$strSQL .= "	email, ";
		$strSQL .= "	taxId, ";
		$strSQL .= "	taxType, ";
		$strSQL .= "	idCountry, ";
		$strSQL .= "	state ";
		$strSQL .= "	) VALUES ( ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["frmDemo_Resto"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["frmDemo_Resto"], "UTF-8")."', ";
		$strSQL .= "	'', ";
		$strSQL .= "	'', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["frmDemo_Phone"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["frmDemo_Phone"], "UTF-8")."', ";
		$strSQL .= "	'', ";
		$strSQL .= "	'".$_REQUEST["frmDemo_EMail"]."', ";
		$strSQL .= "	'".$taxId."', ";
		$strSQL .= "	".$taxType.", ";
		$strSQL .= "	".$idDefaultCountry.", ";
		$strSQL .= "	'".$stateAdd."'";
		$strSQL .= "	)";
		$DB->Execute($strSQL);
		
		$idRestaurant = $DB->Insert_ID();
		
		//Add User
		$strSQL = "INSERT INTO users ( ";
		$strSQL .= "	userName, ";
		$strSQL .= "	userPassword, ";
		$strSQL .= "	userCompleteName, ";
		$strSQL .= "	userEMail, ";
		$strSQL .= "	userPhone, ";
		$strSQL .= "	userCellPhone, ";
		$strSQL .= "	userAddress, ";
		$strSQL .= "	userState, ";
		$strSQL .= "	idRestaurant, ";
		$strSQL .= "	idTypeUser ";
		$strSQL .= "	) VALUES ( ";
		$strSQL .= "	'".mb_strtoupper($userName, "UTF-8")."', ";
		$strSQL .= "	'".$mcrypt->encrypt("123456")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["frmDemo_Name"], "UTF-8")."', ";
		$strSQL .= "	'".$_REQUEST["frmDemo_EMail"]."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["frmDemo_Phone"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["frmDemo_Phone"], "UTF-8")."', ";
		$strSQL .= "	'', ";
		$strSQL .= "	'".$stateAdd."', ";
		$strSQL .= "	'".$idRestaurant."', ";
		$strSQL .= "	".$idTypeAdminResto;
		$strSQL .= "	)";
		$DB->Execute($strSQL);
		
		$idUser = $DB->Insert_ID();
		
		//Make TypeFood List
		$strSQL = "INSERT INTO typefood ( ";
		$strSQL .= "	typeDescription, ";
		$strSQL .= "	state, ";
		$strSQL .= "	idMenuFood, ";
		$strSQL .= "	idRestaurant ) ";
		$strSQL .= "SELECT typeDescription, '$stateAdd', idMenuFood, $idRestaurant FROM typefood WHERE idRestaurant = $idRestaurantDemo";
		$DB->Execute($strSQL);
		
		//Make Food List
		$strSQL = "INSERT INTO food ( ";
		$strSQL .= "	description, ";
		$strSQL .= "	idTypeFood, ";
		$strSQL .= "	price, ";
		$strSQL .= "	state, ";
		$strSQL .= "	idRestaurant ) ";
		$strSQL .= "SELECT description, idTypeFood, price, '$stateAdd', $idRestaurant FROM food WHERE idRestaurant = $idRestaurantDemo";
		$DB->Execute($strSQL);
		
		if (!$DB->CompleteTrans()) {
			$arrJSON->status = "ERR";
			$arrJSON->msg = $a_languages['Mailer.Demo.SendError'];
			return json_encode($arrJSON);
		}
		else{
			//Make addresses
			$addresses = array();
			array_push($addresses, $_REQUEST["frmDemo_EMail"]);
			
			enviarMailConPagina($addresses, $a_languages['Mailer.Demo.Subject'], $basedir . $mailDemoUrl, "id=" . $idUser);
			
			$arrJSON->status = "OK";
			$arrJSON->msg = $a_languages['Mailer.Demo.SendOK'];
			return json_encode($arrJSON);
		}
	}
	catch(Exception $e) {
		$arrJSON->status = "ERR";
		$arrJSON->msg = $a_languages['Mailer.Demo.SendError'];
		return json_encode($arrJSON);
	}	
}

function sendMailPassword()
{
	require("includes/selectLanguage.php");
	require("includes/constants.php");

	try
	{
		$DB = NewADOConnection('mysqlt');
		$DB->Connect();
		$DB->Execute("SET NAMES utf8;");
		//$DB->debug=true;

		$strSQL = "SELECT * FROM users WHERE UPPER(userEMail) = '".strtoupper($_REQUEST["lostPassEMail"])."'";
		$rsUsers = $DB->Execute($strSQL);

		if($rsUsers->EOF)
		{
			$arrJSON->status = "ERR";
			$arrJSON->msg = $a_languages['Mailer.Password.SendErrorUserNotExits'];
			return json_encode($arrJSON);
		}
						 
		//Make addresses
		$addresses = array();
		array_push($addresses, $_REQUEST["lostPassEMail"]);
		
		enviarMailConPagina($addresses, $a_languages['Mailer.Password.Subject'], $basedir . $mailPasswordUrl, "id=" . $rsUsers->fields('idUser'));
		
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Mailer.Password.SendOK'];
		return json_encode($arrJSON);
			
	}
	catch(Exception $e) {
		$arrJSON->status = "ERR";
		$arrJSON->msg = $a_languages['Mailer.Password.SendError'];
		return json_encode($arrJSON);
	}	
}

switch($_REQUEST['accion'])
{
	case "sendMailContact":
		echo sendMailContact();
		break;
	case "sendMailDemo":
		echo sendMailDemo();
		break;
	case "sendMailPassword":
		echo sendMailPassword();
		break;
}
?>